package com.wangfei.lecture.fanweiyunsuanfu;

import org.junit.Test;

import java.io.PrintStream;
import java.util.Comparator;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Stream;

public class MethodRefTest {

    @Test
    public void test1() {
        Consumer<String> consumer1 = str -> System.out.println(str);
        consumer1.accept("beijing");
        System.out.println("++++++++++++++++++++");

        PrintStream ps = System.out;
        Consumer<String> consumer2 = ps::println;
        consumer2.accept("shenzhen");
    }

    //Supplier中的T get()
    //Employee中的String getName()
    @Test
    public void test2() {
        Employee emp =  new Employee(1001, "Tom", 23, 5600);
        Supplier<String> sup1 = () -> emp.getName();
        System.out.println(sup1.get());
        System.out.println("++++++++++++++++++++");

        Supplier<String> sup2 = emp::getName;
        System.out.println(sup2.get());
    }

    // 情况二：类 :: 静态方法
    //Comparator中的int compare(T t1,T t2)
    //Integer中的int compare(T t1,T t2)
    @Test
    public void test3(){
        Comparator<Integer> com1 = (t1, t2) -> Integer.compare(t1, t2);
        System.out.println(com1.compare(1, 100));

        Comparator<Integer> com2 = Integer::compare;
        System.out.println(com1.compare(1000, 100));
    }

    @Test
    public void test4(){
        Function<Double, Long> func = new Function<Double, Long>() {
            @Override
            public Long apply(Double aDouble) {
                return Math.round(aDouble);
            }
        };
        System.out.println(func.apply(10.1));

        Function<Double, Long> func1 = a -> Math.round(a);
        System.out.println(func1.apply(10.6));

        Function<Double, Long> func2 = Math::round;
        System.out.println(func1.apply(12.6));
    }

    //构造器引用
    //Supplier中的T get()
    //Employee的空参构造器：Employee()
    @Test
    public void test5() {
        Supplier<Employee> sup1 = new Supplier<Employee>() {
            @Override
            public Employee get() {
                return new Employee();
            }
        };
        System.out.println(sup1.get());

        Supplier<Employee> sup2 = () -> new Employee();
        System.out.println(sup2.get());

        Supplier<Employee> sup3 = Employee::new;
        System.out.println(sup3.get());
    }

    @Test
    public void test6() {
        //Stream.iterate(0, t->t+2).limit(10).forEach(System.out::println);
        Stream.generate(Math::random).limit(10).forEach(System.out::println);
    }
}
